package com.zyl.blog.common.domain.rbac.vo.perm;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.blog.common.config.TreeConfig;
import com.zyl.blog.common.domain.rbac.bean.Permission;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: Personal_Blog_Server
 * @Package: com.zyl.blog.common.domain.rbac.vo.perm
 * @ClassName: PermVO
 * @Description:
 * @Author FinalFantasy
 * @Date 2025/9/3-18:07
 */
@Data
public class PermVO implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    public static final TreeConfig<PermVO, BigInteger> TREE_CONFIG = TreeConfig.of(
            PermVO::getPermissionId,
            PermVO::getParentId,
            PermVO::getChildren,
            PermVO::setChildren,
            Comparator.comparing(PermVO::getPermissionId)
    );

    /** 权限ID */
    @JsonProperty("id")
    private BigInteger permissionId;

    /** 权限名称 */
    @JsonProperty("name")
    private String permissionName;

    /** 权限标识 */
    @JsonProperty("key")
    private String permissionKey;

    /** 权限类型（1=菜单，2=按钮） */
    @JsonProperty("type")
    private Integer permissionType;

    /** 权限URL */
    @JsonProperty("path")
    private String path;

    /** 权限图标 */
    @JsonProperty("icon")
    private String icon;

    /** 排序 */
    @JsonProperty("sort")
    private Integer sort;

    /** 权限状态： 0-禁用, 1-正常 */
    @JsonProperty("status")
    private Integer status;

    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JsonProperty(value = "createTime", access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime createTime;

    /** 更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JsonProperty(value = "updateTime", access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime updateTime;

    /** 逻辑删除：0=正常，1=已删除 */
    @JsonIgnore
    @TableLogic
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Integer deleted;

    /** 父权限ID */
    @JsonProperty("parentId")
    private BigInteger parentId;

    /** 子角色列表 */
    @JsonProperty("children")
    private List<PermVO> children;

    public static List<PermVO> toVO(List<Permission> permissions) {
        return BeanUtil.copyToList(permissions, PermVO.class);
    }
}
